# —*— coding: utf-8 -*-


from openpyxl.workbook.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

from openpyxl.chart.title import Title
from openpyxl.chart.layout import Layout, ManualLayout
from openpyxl.chart.legend import Legend, LegendEntry
from openpyxl.chart.series import Series, Marker, DataPoint
from openpyxl.chart.axis import (
    _BaseAxis,
    TextAxis,
    NumericAxis,
    SeriesAxis,
    ChartLines,
    Scaling,
)

from openpyxl.chart.label import DataLabel, DataLabelList
from openpyxl.chart.shapes import GraphicalProperties
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import (
    Font,
    Paragraph,
    ParagraphProperties,
    CharacterProperties,
    LineProperties,
    RichTextProperties
)

from openpyxl.styles import numbers

"""


"""


def draw_title(title: Title):
    """
    标题
    :param title:
    :return:
    """
    if title is None:
        title = Title()
        pass
    title.text = '标题'
    # title.overlay = 100

    draw_spPr(title.spPr)
    draw_txPr(title.txPr)

    pass


def draw_layout(layout: Layout):
    """
    布局
    :param layout:
    :return:
    """
    manualLayout = layout.manualLayout
    if manualLayout is None:
        manualLayout = ManualLayout()
        pass
    pass


def draw_legend(legend: Legend):
    """
    图例
    :param legend:
    :return:
    """
    # position=legendPos:['b', 'tr', 'l', 'r', 't']
    legend.position = 'tr'
    draw_spPr(legend.spPr)
    draw_txPr(legend.txPr)
    legendEntry = legend.legendEntry
    if legendEntry is None:
        legendEntry = LegendEntry()
        pass
    pass


def draw_series(sheet: Worksheet, series: Series):
    """系列"""

    series.spPr = GraphicalProperties()
    # draw_spPr(series.spPr)

    marker: Marker = series.marker
    # draw_spPr(marker.spPr)

    # labels=dLbls:DataLabelList
    series.dLbls = DataLabelList()
    dLbls = series.dLbls
    # 分类
    dLbls.showCatName = True
    # 类别
    dLbls.showSerName = True
    # 引导线
    dLbls.showLeaderLines = True
    # 显示数值或百分比
    dLbls.showVal = True
    # dLbls.showPercent = True
    dLbls.numFmt = numbers.FORMAT_PERCENTAGE_00

    # position=dLblPos:['bestFit', 'b', 'ctr', 'inBase', 'inEnd', 'l', 'outEnd', 'r', 't']
    dLbls.position = 'bestFit'

    # dLbl:list(DataLabel)
    dLbls.dLbl = [DataLabel(idx=i) for i in range(sheet.max_row - 1)]
    data_label = dLbls.dLbl[-1]
    data_label.showVal = True
    data_label.dLblPos = 't'

    # data_points=dPt:list(DataPoint)
    series.dPt = [DataPoint(idx=i) for i in range(sheet.max_row - 1)]
    data_point = series.dPt[-1]
    # draw_spPr(data_point.spPr)

    pass


def draw_marker(marker: Marker):
    """标记"""
    marker.spPr.ln.prstDash = 'sysDash'
    marker.spPr.ln.solidFill = 'FF0000'
    marker.spPr.solidFill = 'FF0000'
    marker.symbol = 'circle'
    marker.size = 5
    pass


def draw_axis(axis: _BaseAxis):
    """坐标轴"""
    draw_title(axis.title)
    draw_spPr(axis.spPr)
    draw_txPr(axis.txPr)

    # 刻度范围
    scaling: Scaling = axis.scaling
    scaling.min = 0
    scaling.max = 10

    # ['cross', 'in', 'out']
    axis.majorTickMark = 'out'

    # ['high', 'low', 'nextTo']
    axis.tickLblPos = 'low'
    # ['b', 'l', 'r', 't']
    axis.axPos = 'l'

    # number_format = numFmt
    axis.number_format = numbers.FORMAT_PERCENTAGE_00

    # axis.delete = True
    # axis.majorGridlines = None

    pass


def draw_x_axis(x_axis: TextAxis):
    """
    横轴（X轴）
    :param x_axis:
    :return:
    """
    draw_axis(x_axis)
    # ['ctr', 'l', 'r']
    x_axis.lblAlgn = 'l'
    # 0~1000
    x_axis.lblOffset = 10

    # 刻度标签间隔
    x_axis.tickLblSkip = 5
    # 刻度标记间隔
    x_axis.tickMarkSkip = 5

    # 字体设置
    font = Font(typeface='微软雅黑')
    cp = CharacterProperties(ea=font, cs=font, sz=800)
    rtp = RichTextProperties(rot=-45 * 60000)  # 倾斜-45度
    x_axis.txPr = RichText(bodyPr=rtp, p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])

    pass


def draw_y_axis(y_axis: NumericAxis):
    """
    纵轴（Y轴）
    :param y_axis:
    :return:
    """
    draw_axis(y_axis)
    pass


def draw_z_axis(z_axis: SeriesAxis):
    """竖轴"""
    draw_axis(z_axis)
    pass


def draw_spPr(spPr: GraphicalProperties):
    """
    图形
    spPr = graphicalProperties
    :param spPr:
    :return:
    """
    if spPr is None:
        spPr = GraphicalProperties()
        pass
    # spPr.noFill = True
    # spPr.solidFill = 'FFFFFF'
    # spPr.gradFill
    # spPr.pattFill

    # 线
    # ln=line:LineProperties
    line = LineProperties(solidFill='D9D9D9')
    spPr.ln = line
    line.algn = None
    line.cap = None
    line.cmpd = None

    pass


def draw_txPr(txPr: RichText):
    """
    文本
    txPr = textProperties
    :param txPr:
    :return:
    """
    if txPr is None:
        txPr = RichText()
        pass
    cp = CharacterProperties(ea=Font(typeface='Times New Roman'), cs=Font(typeface='宋体'))
    pp = ParagraphProperties(defRPr=cp)
    # paragraphs = p:Paragraph
    paragraph = Paragraph(pPr=pp, endParaRPr=cp)
    txPr.paragraphs[0] = paragraph

    # properties = bodyPr:RichTextProperties
    txPr.properties = RichTextProperties(vert='horz')
    # vert=['horz', 'vert', 'vert270', 'wordArtVert', 'eaVert', 'mongolianVert', 'wordArtVertRtl']
    pass


if __name__ == '__main__':
    pass
